package com.hqyj.module.leave.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.module.leave.dao.HolidayDao;
import com.hqyj.module.leave.pojo.Result;
import com.hqyj.module.leave.pojo.SearchBean;
import com.hqyj.module.leave.pojo.HolidayInfo;


import com.hqyj.module.leave.service.HolidayInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Optional;

/**
 * @author ：hyj
 * @description：LeaveInfoServiceImpl
 * @date ：2021/9/28 15:03
 */
@Service
public class HolidayInfoServiceImpl implements HolidayInfoService {

    @Autowired
    HolidayDao holidayDao;

    @Override
    public HolidayInfo getHolidayInfo(Integer userId) {
        return holidayDao.holidayInfo(userId);
    }

    @Override
    public PageInfo<HolidayInfo> getAllHolidayInfo(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(),searchBean.getPageSize());
        return new PageInfo<>(Optional
                            .ofNullable(holidayDao.allHolidayInfo(searchBean))
                            .orElse(Collections.emptyList()));
    }

    @Override
    public Result<HolidayInfo> insertHolidayInfo(HolidayInfo holidayInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
        Date st = new Date(holidayInfo.getStartTime());
        Date et = new Date(holidayInfo.getEndTime());
        String stn = sdf.format(st);
        String etn = sdf.format(et);
        holidayInfo.setStartTime(stn);
        System.out.println(stn);
        holidayInfo.setEndTime(etn);
        holidayDao.insertHolidayInfo(holidayInfo);
        return new Result<HolidayInfo>(Result.ResultCode.SUCCESS.code, "Insert success", holidayInfo);
    }

    @Override
    public Result<HolidayInfo> leaderUpdateHolidayInfo(HolidayInfo holidayInfo) {
        holidayDao.leaderUpdateHolidayInfo(holidayInfo);
        return new Result<HolidayInfo>(Result.ResultCode.SUCCESS.code, "Update success", holidayInfo);
    }

    @Override
    public Result<Object> deleteHolidayInfo(int id) {
        holidayDao.deleteHolidayInfo(id);
        return new Result<Object>(Result.ResultCode.SUCCESS.code,"Delete success");
    }

    @Override
    public Result<HolidayInfo> updateHolidayInfo(HolidayInfo holidayInfo) {
        holidayDao.updateHolidayInfo(holidayInfo);
        return new Result<HolidayInfo>(Result.ResultCode.SUCCESS.code,"Update success",holidayInfo);
    }

}
